package com.jxpanda.spring.module.pay.core.pojo.wechat;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.jxpanda.infrastructure.core.constant.DecimalConstant;
import lombok.*;

import java.math.BigDecimal;

/**
 * @author Panda
 */
@Getter
@Setter
public class WechatPayAmount {

    private static final String CURRENCY_CNY = "CNY";

    @JsonProperty("currency")
    private String currency;
    @JsonProperty("total")
    private int total;
    @JsonProperty("refund")
    private int refund;
    @JsonProperty("payer_total")
    private int payerTotal;
    @JsonProperty("payer_refund")
    private int payerRefund;
    @JsonProperty("settlement_refund")
    private int settlementRefund;
    @JsonProperty("settlement_total")
    private int settlementTotal;
    @JsonProperty("discount_refund")
    private int discountRefund;
    @JsonProperty("payer_currency")
    private String payerCurrency;


    public static UnifiedOrderAmount unifiedOrderAmount(BigDecimal total) {
        UnifiedOrderAmount unifiedOrderAmount = new UnifiedOrderAmount();
        unifiedOrderAmount.total = transferDecimal(total);
        unifiedOrderAmount.currency = CURRENCY_CNY;
        return unifiedOrderAmount;
    }

    public static RefundAmount refund(BigDecimal total, BigDecimal refund) {
        RefundAmount refundAmount = new RefundAmount();
        refundAmount.total = transferDecimal(total);
        refundAmount.refund = transferDecimal(refund);
        refundAmount.currency = CURRENCY_CNY;
        return refundAmount;
    }

    private static int transferDecimal(BigDecimal decimal) {
        return decimal.multiply(DecimalConstant.ONE_HUNDRED).intValue();
    }

    @Getter
    @Setter
    public static class UnifiedOrderAmount {
        @JsonProperty("currency")
        private String currency;
        @JsonProperty("total")
        private int total;
    }

    @Getter
    @Setter
    public static class RefundAmount {
        @JsonProperty("currency")
        private String currency;
        @JsonProperty("total")
        private int total;
        @JsonProperty("refund")
        private int refund;
    }
}
